/**
 * 
 */
package fr.isima.isipaint.controllers;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JMenuItem;

import fr.isima.isipaint.model.GraphicObject;
import fr.isima.isipaint.model.Scene;
import fr.isima.isipaint.tools.NewAction;
import fr.isima.isipaint.tools.QuitAction;

/**
 * @author daniel
 * 
 */
public class ToolsController implements ActionListener {

	/** The model */
	private Scene scene;

	public ToolsController(Scene scene) {
		this.scene = scene;
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() instanceof JButton) {
			AbstractAction command = (AbstractAction) ((JButton) e.getSource())
					.getAction();

			command.actionPerformed(e);

			if (command.getValue("GRAPHIC") != null) {
				scene.setCurrent((GraphicObject) command.getValue("GRAPHIC"));
				scene.setSelection(false);
				scene.setFilledAction(false);
				scene.setDrawAction(true);
			} else if (command.getValue("FILL") != null) {
				scene.setColor((Color) command.getValue("FILL"));
				scene.setSelection(false);
				scene.setFilledAction(true);
				scene.setDrawAction(false);

			} else {
				scene.setCurrent(null);
				scene.setSelection(true);
				scene.setFilledAction(false);
				scene.setDrawAction(false);
			}

		} else if (e.getSource() instanceof JMenuItem) {
			AbstractAction command = (AbstractAction) ((JMenuItem) e
					.getSource()).getAction();

			if (command instanceof NewAction) {
				scene.reset();
			} else if (command instanceof QuitAction) {
				System.out.println("Exit");
				System.exit(0);
			}
		}

	}
}
